<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>协程实现之调度器 | 协程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter8/8_0.html" />
    
    
    <link rel="prev" href="../chapter6/6_0.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="7"
        data-chapter-title="协程实现之调度器"
        data-filepath="chapter7/7_0.md"
        data-basepath=".."
        data-revision="Sat Oct 13 2018 10:54:16 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="http://www.0voice.com" target="blank" class="custom-link">ntyTcp</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        前言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/1_0.html">
            
                
                    <a href="../chapter1/1_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        协程的起源
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/2_0.html">
            
                
                    <a href="../chapter2/2_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        协程的案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/3_0.html">
            
                
                    <a href="../chapter3/3_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        协程的实现之工作原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter4/4_0.html">
            
                
                    <a href="../chapter4/4_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        协程的实现之原语操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter5/5_0.html">
            
                
                    <a href="../chapter5/5_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        协程实现之切换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/6_0.html">
            
                
                    <a href="../chapter6/6_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        协程实现之定义
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="7" data-path="chapter7/7_0.html">
            
                
                    <a href="../chapter7/7_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        协程实现之调度器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter8/8_0.html">
            
                
                    <a href="../chapter8/8_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        协程性能测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="chapter9/9_0.html">
            
                
                    <a href="../chapter9/9_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        协程多核模式
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >协程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="7&#x534F;&#x7A0B;&#x5B9E;&#x73B0;&#x4E4B;&#x8C03;&#x5EA6;&#x5668;">7.&#x534F;&#x7A0B;&#x5B9E;&#x73B0;&#x4E4B;&#x8C03;&#x5EA6;&#x5668;</h1>
<ul>
<li><a href="#1">&#x524D;&#x8A00;</a></li>
<li><a href="#2">7.1.&#x751F;&#x4EA7;&#x8005;&#x6D88;&#x8D39;&#x8005;&#x6A21;&#x5F0F;</a></li>
<li><a href="#3">7.2.&#x591A;&#x72B6;&#x6001;&#x4E0B;&#x8FD0;&#x884C;</a></li>
</ul>
<h3 id="1">&#x524D;&#x8A00;</h3>  

<p>&#x2003;&#x2003;<strong>&#x95EE;&#x9898;&#xFF1A;&#x534F;&#x7A0B;&#x5982;&#x4F55;&#x88AB;&#x8C03;&#x5EA6;&#xFF1F;</strong></p>
<p>&#x2003;&#x2003;<strong>&#x8C03;&#x5EA6;&#x5668;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6848;&#xFF0C;&#x4E00;&#x79CD;&#x662F;&#x751F;&#x4EA7;&#x8005;&#x6D88;&#x8D39;&#x8005;&#x6A21;&#x5F0F;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x591A;&#x72B6;&#x6001;&#x8FD0;&#x884C;&#x3002;</strong></p>
<h3 id="2">7.1.&#x751F;&#x4EA7;&#x8005;&#x6D88;&#x8D39;&#x8005;&#x6A21;&#x5F0F;</h3>

<p><img src="../image/8_1.png" alt="&#x751F;&#x4EA7;&#x8005;&#x6D88;&#x8D39;&#x8005;&#x6A21;&#x5F0F;"></p>
<p>&#x2003;&#x2003;&#x903B;&#x8F91;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>while (1) {

        //&#x904D;&#x5386;&#x7761;&#x7720;&#x96C6;&#x5408;&#xFF0C;&#x5C06;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x7684;&#x52A0;&#x5165;&#x5230;ready
        nty_coroutine *expired = NULL;
        while ((expired = sleep_tree_expired(sched)) != ) {
            TAILQ_ADD(&amp;sched-&gt;ready, expired);
        }

        //&#x904D;&#x5386;&#x7B49;&#x5F85;&#x96C6;&#x5408;&#xFF0C;&#x5C06;&#x6EE1;&#x8DB3;&#x6DFB;&#x52A0;&#x7684;&#x52A0;&#x5165;&#x5230;ready
        nty_coroutine *wait = NULL;
        int nready = epoll_wait(sched-&gt;epfd, events, EVENT_MAX, 1);
        for (i = 0;i &lt; nready;i ++) {
            wait = wait_tree_search(events[i].data.fd);
            TAILQ_ADD(&amp;sched-&gt;ready, wait);
        }

        // &#x4F7F;&#x7528;resume&#x56DE;&#x590D;ready&#x7684;&#x534F;&#x7A0B;&#x8FD0;&#x884C;&#x6743;
        while (!TAILQ_EMPTY(&amp;sched-&gt;ready)) {
            nty_coroutine *ready = TAILQ_POP(sched-&gt;ready);
            resume(ready);
        }
    }
</code></pre><h3 id="3">7.2.&#x591A;&#x72B6;&#x6001;&#x4E0B;&#x8FD0;&#x884C;</h3>

<p><img src="../image/8_2.png" alt="&#x591A;&#x72B6;&#x6001;&#x4E0B;&#x8FD0;&#x884C;"></p>
<p>&#x2003;&#x2003;&#x903B;&#x8F91;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>while (1) {

        //&#x904D;&#x5386;&#x7761;&#x7720;&#x96C6;&#x5408;&#xFF0C;&#x4F7F;&#x7528;resume&#x6062;&#x590D;expired&#x7684;&#x534F;&#x7A0B;&#x8FD0;&#x884C;&#x6743;
        nty_coroutine *expired = NULL;
        while ((expired = sleep_tree_expired(sched)) != ) {
            resume(expired);
        }

        //&#x904D;&#x5386;&#x7B49;&#x5F85;&#x96C6;&#x5408;&#xFF0C;&#x4F7F;&#x7528;resume&#x6062;&#x590D;wait&#x7684;&#x534F;&#x7A0B;&#x8FD0;&#x884C;&#x6743;
        nty_coroutine *wait = NULL;
        int nready = epoll_wait(sched-&gt;epfd, events, EVENT_MAX, 1);
        for (i = 0;i &lt; nready;i ++) {
            wait = wait_tree_search(events[i].data.fd);
            resume(wait);
        }

        // &#x4F7F;&#x7528;resume&#x6062;&#x590D;ready&#x7684;&#x534F;&#x7A0B;&#x8FD0;&#x884C;&#x6743;
        while (!TAILQ_EMPTY(sched-&gt;ready)) {
            nty_coroutine *ready = TAILQ_POP(sched-&gt;ready);
            resume(ready);
        }
    }
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter6/6_0.html" class="navigation navigation-prev " aria-label="Previous page: 协程实现之定义"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter8/8_0.html" class="navigation navigation-next " aria-label="Next page: 协程性能测试"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"theme-default":{"showLevel":false},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
